<?php
/**
 * provide common methods to its subclasses
 *
 * @author     ben
 */
abstract class WacStatsAction extends WacCommonActions
{
    /*
     * get date periods according to the request params
     * @return array
     */
//    public function getPeriods($request, $interval='month')
//    {
//        $periods = DatePeriod::getPeriods(false, $interval);
//
//        return $periods;
//    }

    public function getPeriods($objRequest, $interval='month')
    {
        $periods = array();
        $strStartDate = $objRequest->getParameter("start_year").'-'.$objRequest->getParameter("start_month").'-'.$objRequest->getParameter("start_day");
        $strEndDate   = $objRequest->getParameter("end_year").'-'.$objRequest->getParameter("end_month").'-'.$objRequest->getParameter("end_day");
        $startDate = new DateTime($strStartDate);
        $endDate = new DateTime($strEndDate);

        switch($interval){
            case "day":
                $objInterval = new DateInterval('P0Y0M1DT0H0M0S');
                break;
            case "week":
                $objInterval = new DateInterval('P0Y0M1WT0H0M0S');
                break;
            case "month":
//                $objInterval = new DateInterval('P0Y1M0WT0H0M0S');
                $objInterval = DateInterval::createFromDateString('first day of next month');
                break;
            case "year":
                $objInterval = new DateInterval('P1Y');
//                $objInterval = DateInterval::createFromDateString('last month of next year');
                break;
            default:
                $objInterval = DateInterval::createFromDateString('last day of next month');
                break;
        }

        $midPeriods = new DatePeriod($startDate, $objInterval, $endDate);

        $i=0;
        $periodPair = array();
        foreach ( $midPeriods as $mp ) {
            if($i>0){
                $periodPair["start_date"] = $periodPair["end_date"];
                $periodPair["end_date"]   = $mp->format( "Y-m-d H:i:s" );
                $periods[] = $periodPair;
            }
            else{
                $periodPair["start_date"] = $periodPair["end_date"] = $mp->format( "Y-m-d H:i:s" );
            }
            $i++;
        }
        $periodPair["start_date"] = $periodPair["end_date"];
        $periodPair["end_date"]   = $endDate->format( "Y-m-d H:i:s" );
        $periods[] = $periodPair;
        
        return $periods;
    }

    public function _outputBar3DSWFTest($request)
    {
        //Create new stBar3D object and set the transparency and colour.
		$redBar = new stBar3D( 75, '#d01f3c' );
		$redBar->key( '2007', 10 );

		//random data
		for( $i = 0; $i < 12; $i++ )
		{
			$redBar->data[] = rand(200,500);
		}

		//2nd Bar
		$blueBar = new stBar3D( 75, '#356aa0' );
		$blueBar->key( '2008', 10 );

		//random data for 2nd bar
		for( $i = 0; $i < 12; $i++ )
		{
			$blueBar->data[] = rand(200,500);
		}

        //3nd Bar
		$greenBar = new stBar3D( 75, '#80FF00' );
		$greenBar->key( '2009', 10 );

		//random data for 3nd bar
		for( $i = 0; $i < 12; $i++ )
		{
			$greenBar->data[] = rand(200,500);
		}

		$g = new stGraph();
		$g->bg_colour = '#E4F5FC';
		$g->title( 'Number of downloads in 2008 and 2009', '{font-size:20px; color: #18A6FF;}' );

		$g->data_sets[] = $redBar;
		$g->data_sets[] = $blueBar;
                $g->data_sets[] = $greenBar;

		//to create 3d x-axis
		$g->set_x_axis_3d( 10 );
		$g->x_axis_colour( '#8499A4', '#E4F5FC' );
		$g->y_axis_colour( '#8499A4', '#E4F5FC' );

//		$g->set_x_labels( array( 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct', 'Nov', 'Dec' ) );
                $g->set_x_labels( array( '1月','2月','3月','4月','5月','6月','7月','8月','9月','10月', '11月', '12月' ) );
		$g->set_y_max( 500 );
		$g->y_label_steps( 5 );
		$g->set_y_legend( 'stOfcPlugin', 12, '#18A6FF' );
		echo $g->render();

		return sfView::NONE;
    }
}
